package com.dhc.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhc.service.entity.Academy;
import com.dhc.service.entity.Profession;
import com.dhc.service.entity.Student;
import com.dhc.service.entity.dto.ModifyStudentDto;
import com.dhc.service.entity.dto.StudentFuzzyDto;
import com.dhc.service.entity.vo.AcademyVo;
import com.dhc.service.entity.vo.ProfessionVo;
import com.dhc.service.entity.vo.StudentVo;
import com.dhc.service.mapper.AcademyMapper;
import com.dhc.service.mapper.ProfessionMapper;
import com.dhc.service.mapper.StudentMapper;
import com.dhc.service.service.StudentService;
import lombok.NonNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【STUDENT(学生表)】的数据库操作Service实现
 * @createDate 2023-10-08 18:48:41
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
		implements StudentService {
	
	@Autowired
	private StudentMapper studentMapper;

	@Autowired
	private ProfessionMapper professionMapper;

	@Autowired
	private AcademyMapper academyMapper;
	
	@Override
	public List<StudentVo> fullQuery() {
		List<StudentVo> studentVoList = studentMapper.fullQuery();
		for (StudentVo studentVo : studentVoList) {
			Long stuProfessionId = studentVo.getStuProfessionId();
			Profession profession = professionMapper.selectById(stuProfessionId);
			ProfessionVo professionVo = new ProfessionVo();
			BeanUtils.copyProperties(profession, professionVo);
			studentVo.setProfessionVo(professionVo);
			Long proAcaId = profession.getProAcaId();
			Academy academy = academyMapper.selectById(proAcaId);
			AcademyVo academyVo = new AcademyVo();
			BeanUtils.copyProperties(academy, academyVo);
			studentVo.setAcademyVo(academyVo);
		}
		return studentVoList;
	}
	
	@Override
	public List<StudentVo> fuzzyQuery(@NonNull StudentFuzzyDto fuzzyDto) {
		return studentMapper.fuzzyQuery(fuzzyDto);
	}

	@Override
	public boolean addData(ModifyStudentDto modifyStudentDto) {
		Student student = new Student();
		BeanUtils.copyProperties(modifyStudentDto, student);
		QueryWrapper<Profession> professionQueryWrapper = new QueryWrapper<>();
		professionQueryWrapper.eq("PRO_NAME", modifyStudentDto.getProName());
		Profession profession = professionMapper.selectOne(professionQueryWrapper);
		Long proId = profession.getProId();
		student.setStuProfessionId(proId);
		return studentMapper.insert(student) > 0;
	}

	@Override
	public boolean updateData(ModifyStudentDto modifyStudentDto) {
		Student student = new Student();
		BeanUtils.copyProperties(modifyStudentDto, student);
		QueryWrapper<Profession> professionQueryWrapper = new QueryWrapper<>();
		professionQueryWrapper.eq("PRO_NAME", modifyStudentDto.getProName());
		Profession profession = professionMapper.selectOne(professionQueryWrapper);
		Long proId = profession.getProId();
		student.setStuProfessionId(proId);
		return studentMapper.updateById(student) > 0;
	}

	@Override
	public boolean deleteData(String id) {
		return studentMapper.deleteById(id) > 0;
	}
}




